package com.zd.commodity.productMng.biz.impl;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zd.commodity.biz.impl.BaseBiz;
import com.zd.commodity.productMng.biz.IProductAttrDictBiz;
import com.zd.commodity.productMng.dao.IProductAttrDictDao;
import com.zd.commodity.productMng.entity.ProductAttrDict;
import com.zd.commodity.userMng.entity.User;

@Service("productAttrDictBiz")
public class ProductAttrDictBizImpl extends BaseBiz<ProductAttrDict> implements IProductAttrDictBiz {

	private IProductAttrDictDao productAttrDictDao;

	public IProductAttrDictDao getProductAttrDictDao() {
		return productAttrDictDao;
	}

	@Autowired
	public void setProductAttrDictDao(IProductAttrDictDao productAttrDictDao) {
		this.productAttrDictDao = productAttrDictDao;
	    this.setBaseDao(productAttrDictDao);
	}

	@Override
	public void deleteAttrDictByAttrId(String attrId) {
		productAttrDictDao.deleteAttrDictByAttrId(attrId);
		
	}

	/*@Override
	public void updateAttrDict(ProductAttrDict t1,
			List<ProductAttrDict> attrDicList,User user) {
		List<ProductAttrDict> list = productAttrDictDao.getList(t1, null, null);
		//将数据库中的数据字典转换成键是type,值是ProductAttrDict的map
		HashMap<String,ProductAttrDict> map = new HashMap();
		for (ProductAttrDict productAttrDictInDb : list) {
			map.put(productAttrDictInDb.getType(), productAttrDictInDb);
		}
		//遍历前台传入的list
		for(int i=0;i<attrDicList.size();i++){
			ProductAttrDict updateDict = attrDicList.get(i);
			//前台传入value的是否是空
			boolean b = updateDict.getValue()==null||"".equals(updateDict.getValue());
			//数据库已存在则更新
			ProductAttrDict dict = map.get(String.valueOf(i+1));
			Timestamp timestamp = new Timestamp(System.currentTimeMillis());
			if(dict!=null){
				//value为空则删除
				if(b){
					productAttrDictDao.delete(dict.getId());
				}else{
					if(!dict.getValue().equals(updateDict.getValue())){
						dict.setUpdater(user.getUsername());
						dict.setUpdateTime(timestamp);
						dict.setValue(updateDict.getValue());
						productAttrDictDao.update(dict);
					}
				}
			}else{
				if(!b){
					//数据库不存在则插入
					ProductAttrDict newDict = new ProductAttrDict();
					newDict.setCreationTime(timestamp);
					newDict.setCreator(user.getUsername());
					newDict.setProductAttr(t1.getProductAttr());
					newDict.setType(String.valueOf(i+1));
					newDict.setValue(updateDict.getValue());
					productAttrDictDao.add(newDict);
				}
			}
		}
	}*/
	
}
